//201902104011王雪梦
package bysj.service;
import bysj.dao.ActorDao;
import bysj.dao.TeacherDao;
import bysj.dao.UserDao;
import bysj.domain.*;
import bysj.domain.authority.Actor;
import util.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class TeacherService {
	private static TeacherDao teacherDao= TeacherDao.getInstance();
	private static TeacherService teacherService=null;
	private TeacherService(){}
	
	public static TeacherService getInstance(){
		if(teacherService==null){
			teacherService = new TeacherService();
		}
		return teacherService;
	}
	
	public Collection<Teacher> findAll(List <Condition> conditionList, Pagination pagination) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Collection<Teacher> teachers = teacherDao.findAll(connection,conditionList,pagination);
		JdbcHelper.close(connection);
		return teachers;
	}
	
	public Teacher find(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Teacher teacher = teacherDao.find(id,connection);
		JdbcHelper.close(connection);
		return teacher;
	}
	
	public boolean add(Teacher teacher) throws SQLException, BysjException {
		//获得进行事务的连接，添加教师和添加用户都使用本连接对象
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false，开始事务
		connection.setAutoCommit(false);
		boolean added = false;
		try{
			int actorId = ActorDao.getInstance().add(connection);
			//为教师的id赋值
			teacher.setId(actorId);
			//teacherDao添加教师对象，返回值为自动生成的id
			teacherDao.add(teacher,connection);
			//创建用户对象，密码和用户名取自teacher字段值no
			User user = new User(teacher.getNo(),teacher.getNo(),null,teacher);
			System.out.println(user);
			//增加用户
			UserDao.getInstance().add(user,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e){
			System.out.println(e.getMessage());
			if(e.getMessage().equals("Duplicate entry '01' for key 'username'")){
				throw new BysjException("新添加的教师的编号已经存在");
			}
			e.printStackTrace();
			//回滚事务中的所有操作
			connection.rollback();
		}finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null,connection);
		}
		return added;
	}
	public boolean update(Teacher teacher) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean updated = teacherDao.update(teacher,connection);
		JdbcHelper.close(connection);
		return updated;
	}
	public boolean delete(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean deleted = teacherDao.delete(id,connection);
		JdbcHelper.close(connection);
		return deleted;
	}
	public static void main(String[] args) throws SQLException, BysjException {
		ProfTitle profTitle = ProfTitleService.getInstance().find(1);
		Degree degree = DegreeService.getInstance().find(1);
		Department department = DepartmentService.getInstance().find(1);
		//Department department1 = DepartmentService.getInstance().find(2);
		//创建Teacher对象，并向数据库中添加teacher
		Teacher teacher = new Teacher("name01", "name01",profTitle,degree,department);
		System.out.println(teacher);
		TeacherService.getInstance().add(teacher);
	}
}
